//输入某二叉树的前序遍历和中序遍历的结果，请构建该二叉树并返回其根节点。 
//
// 假设输入的前序遍历和中序遍历的结果中都不含重复的数字。 
//
// 
//
// 示例 1: 
//
// 
//Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
//Output: [3,9,20,null,null,15,7]
// 
//
// 示例 2: 
//
// 
//Input: preorder = [-1], inorder = [-1]
//Output: [-1]
// 
//
// 
//
// 限制： 
//
// 0 <= 节点个数 <= 5000 
//
// 
//
// 注意：本题与主站 105 题重复：https://leetcode-cn.com/problems/construct-binary-tree-from-
//preorder-and-inorder-traversal/ 
// Related Topics 树 数组 哈希表 分治 二叉树 👍 595 👎 0


package leetcode.editor.cn1;

import com.leetcode.entity.TreeNode;

//Java：重建二叉树
public class ZhongJianErChaShuLcof {
    public static void main(String[] args) {
        Solution solution = new ZhongJianErChaShuLcof().new Solution();
        TreeNode treeNode = solution.buildTree(new int[]{1,2,3}, new int[]{3,2,1});
        System.out.println(treeNode.val);
        treeNode = solution.buildTree(new int[]{3,9,20,15,7}, new int[]{9,3,15,20,7});
        System.out.println(treeNode.val);
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        /**
         * @param preorder 前序遍历结果：根左右
         * @param inorder  中序遍历结果：左根右
         * @return
         */
        public TreeNode buildTree(int[] preorder, int[] inorder) {
            if (preorder.length == 0)
                return null;
            if (preorder.length != inorder.length)
                return null;
            int rootVal = preorder[0];
            TreeNode root = new TreeNode(rootVal);

            int rootIndex = -1;
            int len = inorder.length;
            for (int i = 0; i < len; i++) {
                if (inorder[i] == rootVal) {
                    rootIndex = i;
                    break;
                }
            }
            // 前序遍历的左子树和中序遍历的左子树
            // 子数组范围: 前序遍历的子数组[1, rootIndex]，中序遍历的子数组
            int prestart = 1;
            int preend = prestart + rootIndex - 1;
            int instart = 0;
            int inend = instart + rootIndex - 1;
            root.left = buildSubTree(preorder, inorder, prestart, preend, instart, inend);
            root.right = buildSubTree(preorder, inorder, preend + 1, len - 1, rootIndex + 1, len - 1);
            return root;
        }

        /**
         * 构建子树
         * // 如何构建子树
         *
         * @param preorder
         * @param inorder
         * @param prestart
         * @param preend
         * @param instart
         * @param inend
         * @return
         */
        public TreeNode buildSubTree(int[] preorder, int[] inorder, int prestart, int preend, int instart, int inend) {
            if (preend == prestart)
                return new TreeNode(preorder[prestart]);

            if (preend > prestart) {
                int nodeVal = preorder[prestart];
                TreeNode node = new TreeNode(nodeVal);

                int nodeIndex = -1;
                for (int i = instart; i < inend + 1; i++) {
                    if (inorder[i] == nodeVal) {
                        nodeIndex = i;
                        break;
                    }
                }

                // 根左右，左根右
                // 数组长度len = nodeIndex - instart
                node.left = buildSubTree(preorder, inorder, prestart + 1, nodeIndex - instart + prestart, instart, nodeIndex - 1);
                node.right = buildSubTree(preorder, inorder, nodeIndex - instart + prestart + 1, preend, nodeIndex + 1, inend);
                return node;
            }
            return null;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
